///index.js
//获取应用实例
var util = require('../../utils/util.js');
var app = getApp();
var listArray = [];
Page({
  data: {
    seecomment: false,
    reply: '',
    phone_num: '',
    login_pwd:'',
    login:'',
    isAgree:false,
    hiddenmodalput:true,
    userList:'',
    index:0,
    userId:'',
    landId:'',
  },
  login_pwd:function(e){
    this.setData({
      login_pwd: e.detail.value
    })

  },
  phone_num: function (e) {
    
    this.setData({
      phone_num: e.detail.value
    })

  },
  forgetPassword:function(){
    wx.navigateTo({
      url: '/pages/forgetPwd/forgetPwd'
    })
  },
  login:function(e){
    var that = this;
    if (!(/^1[3456789]\d{9}$/.test(this.data.phone_num))) {
      wx.showModal({
        title: '错误',
        content: '电话号码格式不正确',
        mode: true,
        showCancel: false,
        confirmColor: '#007aff',
      })
      return false;
    }
    util.req('/app/login', { phone_num: this.data.phone_num, login_pwd: this.data.login_pwd }, function (data) {
      if (data.code == 0) {
        listArray=[];
        var dataLen = data.data.length;
        if (dataLen == 1) {
          var userId = data.data[0].user_id;
          var landId = data.data[0].landId;
          that.fdLoginFilter(userId, landId);
          return;
        }
       
        if (that.data.isAgree){
          that.setchecked(that.data.isAgree);
        }

        for (var i = 0; i < dataLen; i++) {
          var ob = new Object();
            ob.key = data.data[i].landId;
            ob.value = data.data[i].landName+"("+ data.data[i].phone_num+")";
            ob.userId = data.data[i].user_id;
            listArray.push(ob);
          
        }

        that.setData({
          hiddenmodalput:false,
          userList: listArray,
          userId: listArray[0].userId,
          landId: listArray[0].key,
        })
      }else{
        wx.showModal({
          title: '错误',
          content: data.desc,
          mode: true,
          showCancel: false,
          confirmColor: '#007aff',
        })
        
      }
    })
  },
  
  //记住账号点击
  bindAgreeChange: function (e) {
    this.setData({
      isAgree: !!e.detail.value.length
    });
  },
  setchecked:function(e){
    app.globalData.isCheck = this.data.isAgree;
    app.globalData.sk = this.data.phone_num;
    wx.setStorage({
      key: "isCheck",
      data: this.data.isAgree
    })
    wx.setStorage({
      key: "sk",
      data: this.data.phone_num
    })
  },
  termCel:function(e){
      this.setData({
        hiddenmodalput:true,

      })

  },
  termConfirm:function(e){
    var that = this;
    var userId = that.data.userId;
    var landId = that.data.landId;
    that.fdLoginFilter(userId, landId);
  },
  fdLoginFilter: function (userId, landId) {
    var that = this;
    util.req('/app/fdLoginFilter', { userId: userId, landId: landId }, function (data) {
      if (data.code == 0) {
        that.setUserInfo(data);
        if (data.data.user_type_id == 10006) {
          wx.redirectTo({
            url: '/pages/roomPattern/roomPattern',
          })
        } else {
          wx.redirectTo({
            url: '/pages/home/home',
          })
        }
       
      } else {
        wx.showModal({
          title: '错误',
          content: data.desc,
          mode: true,
          showCancel: false,
          confirmColor: '#007aff',
        })
      }
    })
  },
  setUserInfo: function (data) {   //将用户信息缓存保存
    app.globalData.userInfo = data.data;
    
    wx.setStorage({
      key: "userInfo",
      data: data.data
    })
    wx.setStorage({
      key: "sessionId",
      data: data.params.sessionId
    })
  },
  bindPartmentChange:function(e){
    console.log(listArray[e.detail.value].key);
    console.log(listArray[e.detail.value].userId);
    this.setData({
      landId: listArray[e.detail.value].key,
      userId : listArray[e.detail.value].userId,
      index: e.detail.value
    });
   

  },
  onLoad:function(option){
    var that=this;
   wx.getStorage({
      key: 'isCheck',
      success: function (res) {
        app.globalData.isCheck = res.data;
        wx.getStorage({
          key: 'sk',
          success: function (res) {
            app.globalData.sk = res.data;
            if (app.globalData.isCheck && app.globalData.sk) {

              that.setData({
                phone_num: app.globalData.sk,
                isAgree: app.globalData.isCheck
              })
            }
          },
        })
      },
    })
   
  }
})



